package service

import (
	"github.com/google/uuid"
	"github.com/jinzhu/copier"
	"netdisk/consts"
	"netdisk/dao"
	"netdisk/dto"
	"netdisk/models"
	"netdisk/utils"
	"time"
)

func SendEmailToRegister(email string) {
	mm, _ := time.ParseDuration("5m")
	date := time.Now().Add(mm)
	NotExistEmail(email)
	userToken := NonUserToken(email, consts.TokenType2)
	token := uuid.New().String()[:6]
	AddToken(userToken, token, date)
	utils.SendSMS(email, token)
}
func NotExistEmail(email string) {
	user := &models.User{Email: email}
	has, _ := dao.DB.Exist(user)
	if has {
		panic("邮箱已存在")
	}
}
func AddUser(registerDTO *dto.RegisterDTO) {
	UseUserToken(registerDTO.Email, registerDTO.Token, consts.TokenType2)
	if exist, _ := dao.DB.Exist(models.User{Username: registerDTO.UserName}); exist {
		panic("用户名已注册")
	}
	if exist, _ := dao.DB.Exist(models.User{Email: registerDTO.Email}); exist {
		panic("邮箱已使用")
	}
	user := new(models.User)
	copier.Copy(&user, registerDTO)
	user.RoleId = 3
	user.IsAccountNonLocked = 1
	dao.DB.Insert(user)
	userStorage := new(models.UserStorage)
	userStorage.UsedStorage = 0
	userStorage.UserId = user.UserId
	dao.DB.Insert(userStorage)
}
